<template>
  <!-- 充值-->
  <el-dialog
    v-el-drag-dialog
    :title="title"
    :visible.sync="visible"
    width="50%"
    append-to-body
    @close="reset"
  >
    <div class="item">
      <div class="title">
        <span class="title-icon" />
        <span class="title-text">卡信息</span>
      </div>
      <el-descriptions size="medium">
        <el-descriptions-item label="客户编号">{{ csmInfo.csmCode }}</el-descriptions-item>
        <el-descriptions-item label="客户名称">{{ csmInfo.csmName }}</el-descriptions-item>
        <el-descriptions-item label="卡号">{{ csmInfo.cardCode }}</el-descriptions-item>
        <el-descriptions-item label="现金卡值">{{ numberFormat1(csmInfo.cashMoney ) +'元' }}</el-descriptions-item>
        <el-descriptions-item label="赠送卡值">{{ numberFormat1(csmInfo.sendMoney) +'元' }}</el-descriptions-item>
        <el-descriptions-item label="卡余额">{{ numberFormat1(csmInfo.cardSurplus) +'元' }}</el-descriptions-item>
      </el-descriptions>
    </div>
    <!-- 充值 -->
    <div v-if=" type===1 " class="item">
      <div class="title">
        <span class="title-icon" />
        <span class="title-text">其他信息</span>
      </div>
      <el-form ref="dataForm" class="form" label-position="right" label-width="100px">
        <el-form-item label="充值金额:">
          <el-input v-model="temp.feeCount1" placeholder="请输入" @input="onInput($event)" @blur="formatInput($event)" />
        </el-form-item>
        <el-form-item label="赠送金额:">
          <el-input v-model="temp.feeCount2" placeholder="请输入" :disabled="true" />
        </el-form-item>
        <el-form-item label="备注信息:" style="width: 100%;" label-width="90px">
          <el-input v-model="temp.remark" type="textarea" resize="none" placeholder="请输入" />
        </el-form-item>
      </el-form>
    </div>
    <!-- 挂失/冻结/激活 -->
    <div v-if=" type===2 || type===3" class="item">
      <el-form ref="dataForm" class="form" label-position="right" label-width="80px">
        <el-form-item label="备注信息:" style="width: 100%;" label-width="80px">
          <el-input v-model="temp.remark" type="textarea" resize="none" placeholder="请输入" />
        </el-form-item>
      </el-form>
    </div>
    <!-- 换卡-->
    <div v-if=" type===4" class="item">
      <el-form ref="dataForm" class="form" label-position="right" label-width="116px">
        <el-form-item label="换卡工本费:">
          <el-input v-model="temp.cardFee" placeholder="请输入" :disabled="true" />
        </el-form-item>
        <el-form-item label="备注信息:" style="width: 100%;" label-width="100px">
          <el-input v-model="temp.remark" type="textarea" resize="none" placeholder="请输入" />
        </el-form-item>
      </el-form>
    </div>
    <!-- 销卡 -->
    <div v-if=" type===5 " class="item">
      <el-form ref="dataForm" class="form" label-position="right" label-width="100px">
        <el-form-item label="应退金额:">
          <el-input v-model="csmInfo.cashMoney" placeholder="请输入" :disabled="true" />
        </el-form-item>
        <el-form-item label="实退金额:">
          <el-input v-model="temp.money" placeholder="请输入" @input="onInput($event)" @blur="formatInput($event)" />
        </el-form-item>
        <el-form-item label="备注信息:" style="width: 100%;" label-width="90px">
          <el-input v-model="temp.remark" type="textarea" resize="none" placeholder="请输入" />
        </el-form-item>
      </el-form>
    </div>
    <!-- 手机号变更 -->
    <div v-if=" type===6 " class="item">
      <el-form ref="dataForm" class="form" label-position="right" label-width="144px">
        <el-form-item label="原绑定手机号:">
          <el-input v-model="csmInfo.csmPhone" placeholder="请输入" :disabled="true" />
        </el-form-item>
        <el-form-item label="变更后绑定手机号:" label-width="190px">
          <el-select v-model="csmPhone" style="width: 100%;" placeholder="请选择">
            <el-option
              v-for="(i, ind) in phoneItems"
              :key="ind"
              :label="i.csmPhone"
              :value="i.csmPhone"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注信息:" style="width: 100%;" label-width="90px">
          <el-input v-model="temp.remark" type="textarea" resize="none" placeholder="请输入" />
        </el-form-item>
      </el-form>
    </div>
    <!-- 卡密码修改 -->
    <div v-if=" type===7 " class="item">
      <el-form ref="password" class="form" :model="pas" :rules="rules" label-position="right" label-width="150px">
        <el-form-item label="请输入原密码:" style="width: 100%;" label-width="130px" prop="password1">
          <el-input v-model="pas.password1" placeholder="请输入" show-password />
        </el-form-item>
        <el-form-item label="请输入新密码:" prop="password2">
          <el-input v-model="pas.password2" show-password placeholder="请输入" />
        </el-form-item>
        <el-form-item label="再次输入新密码:" label-width="180px" prop="password3">
          <el-input v-model="pas.password3" show-password placeholder="请输入" />
        </el-form-item>
        <el-form-item label="备注信息:" style="width: 100%;" label-width="90px">
          <el-input v-model="temp.remark" type="textarea" resize="none" placeholder="请输入" />
        </el-form-item>
      </el-form>
    </div>
    <!-- 卡密码重置 -->
    <div v-if=" type===8" class="item">
      <el-form ref="dataForm" class="form" label-position="right" label-width="80px">
        <el-form-item label="备注信息:" style="width: 100%;" label-width="80px">
          <el-input v-model="temp.remark" type="textarea" resize="none" placeholder="请输入" />
        </el-form-item>
      </el-form>
    </div>
    <!-- 调账冲正 -->
    <div v-if=" type===9 " class="item">
      <el-form ref="dataForm" class="form" label-position="right" label-width="100px">
        <el-form-item label="调账类型:" label-width="80px" style="width: 100%;">
          <el-radio-group v-model="curtainTy" @change="changeGroup">
            <el-radio :label="1">充值调账</el-radio>
            <el-radio :label="2">消费调账</el-radio>
          </el-radio-group>
          <span>
            <i class="el-icon-question" @click.stop="isText = true" />
            <span v-if="isText" @click.stop="isText = false">
              充值调账:在原基础上增加金额;消费调账:在原基础上减少金额;
            </span>
          </span>
        </el-form-item>
        <el-form-item label="现金卡值调整:" label-width="134px">
          <el-input v-model.trim="price.updateCashMoney" placeholder="请输入" @input="onInput($event, 'updateCashMoney')" @blur="onMoney($event,'updateCashMoney')" />
        </el-form-item>
        <el-form-item label="赠送卡值调整:" label-width="134px">
          <el-input v-model.trim="price.updateSendMoney" placeholder="请输入" @input="onInput($event, 'updateSendMoney')" @blur="onMoney($event,'updateSendMoney')" />
        </el-form-item>
        <el-descriptions size="medium">
          <el-descriptions-item label="调整后现金卡值:">{{ price.changeCashMoney }}</el-descriptions-item>
          <el-descriptions-item label="调整后赠送卡值:">{{ price.changeSendMoney }}</el-descriptions-item>
          <el-descriptions-item label="调整后卡余额:">{{ price.changeCardSurplus }}</el-descriptions-item>
        </el-descriptions>
        <el-form-item label="备注信息:" style="width: 100%;" label-width="80px">
          <el-input v-model="temp.remark" type="textarea" resize="none" placeholder="请输入" />
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="onSubmit">{{ btnTxt }}</el-button>
      <el-button @click="reset">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { SmartReader, FUNCIDS } from '../js/interface'
  import { topUpAdministrationInfoInfo, topUpAdministrationPrice, topUpAdministrationUpdate, topUpAdministrationChangeStatus, topUpAdministrationMelt, topUpAdministrationUserInfo, topUpAdministratioCsmPhone, topUpAdministratioPas, topUpAdministratioReset, topUpAdministratioAccount, topUpAdministratioTradeCrad } from '@/api'
  import { parseTime } from '@/utils'

  export default {
    name: 'crad-operate',
    components: {
    },
    props: {
    },
    data() {
      return {
        parseTime,
        visible: false,
        btnTxt: '充值',
        type: null,
        title: '',
        cardCode: '',
        csmInfo: {},
        phoneItems: [],
        curtainTy: 1, // 调账类型
        csmPhone: '', // 变更后绑定手机号
        isText: true,
        temp: {
          feeCount1: '',
          feeCount2: '0.00',
          remark: '',
          cardFee: 22.22,
          money: ''
        },
        // 修改密码
        pas: {
          password1: '', // 原密码
          password2: '', // 新密码
          password3: '' // 再次输入的新密码
        },
        rules: {
          password1: [{ required: true, message: '请输入原密码', trigger: 'change' }, { min: 6, max: 6, message: '请输入6位数密码', trigger: 'blur' }],
          password2: [{ required: true, message: '请输入新密码', trigger: 'change' }, { min: 6, max: 6, message: '请输入6位数新密码', trigger: 'blur' }],
          password3: [{ required: true, message: '请再次输入新密码', trigger: 'change' }, { min: 6, max: 6, message: '请再次输入6位数新密码', trigger: 'blur' }]
        },
        // 调账
        price: {
          updateCashMoney: 0.00, // 现金卡值调整
          changeCashMoney1: 0.00, // 初始数据现金卡值
          changeSendMoney1: 0.00, // 初始数据赠送卡值调整
          changeCardSurplus1: 0.00, // 初始数据调整后卡余额/余次
          updateSendMoney: 0.00, // 赠送卡值调整
          changeCashMoney: 0.00, // 调整后现金卡值
          changeSendMoney: 0.00, // 调整后赠送卡值
          changeCardSurplus: 0.00// 调整后卡余额
        },
        /* 读卡/写卡相关参数*/
        obj: null, // 读卡器函数
        locked: false,
        csmCardRWType: 0, // 0是默认状态 1是读卡状态 2是写卡状态 3是清卡状态
        readClass: 1,
        writeClass: 2,
        hasCard: false,
        isComOpen: false,
        icdev: -1,
        gl_sector: 0, // 扇区 0-15扇区  总共64个扇区
        gl_BinBlock: 1, // 快   0-63 但0不可用,
        gl_wantFunc: 0,
        readerCardCode: '', // 读卡号
        readerIndex: 1, // 读卡第一扇区值
        readerStrCode: '', // 读卡第二扇区值
        writerCsmCard: '', // 写卡号
        writerNowDate: '' // 充值卡第二扇区值
      }
    },
    computed: {
    },
    created() {},
    methods: {
      async open(data) {
        console.log('ddddd', data)
        const { cardCode, type, title } = data
        const obj = {
          1: '充值',
          2: '激活',
          3: '冻结',
          4: '换卡',
          5: '销卡',
          6: '保存',
          7: '保存',
          8: '重置',
          9: '调账'
        }
        this.btnTxt = obj[type]
        this.type = type
        this.title = title
        await Promise.all([this.getUserInfo(cardCode), this.initCardServer()])
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
        })
      },
      // 提交
      async onSubmit() {
        switch (this.type * 1) {
        case 0:
          break
        case 1: // 充值
          this.$confirm('确认充值金额？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(async() => {
            const feeCount = (this.temp.feeCount1 * 1) + (this.temp.feeCount2 * 1)
            const params = {
              actUser: this.csmInfo.csmName,
              actUserId: this.csmInfo.csmId,
              cardCode: this.csmInfo.cardCode,
              rcType: this.csmInfo.rcType,
              cardSurplus: this.csmInfo.cardSurplus,
              accCsmcardRecordList: [
                {
                  cardCode: this.csmInfo.cardCode,
                  feeCount: feeCount * 100,
                  feeCount1: this.temp.feeCount1 * 100,
                  feeCount2: this.temp.feeCount2 * 100,
                  singleMoney: this.temp.feeCount1 * 100,
                  remark: this.temp.remark
                }
              ]
            }
            const res = await topUpAdministrationUpdate(params)
            if (res.status === 200) {
              this.$message({
                type: 'success',
                message: '充值余额成功！'
              })
              this.visible = false
            }
          })
          break
        case 2: // 激活
        case 3: // 冻结
          var params1 = {
            cardCode: this.csmInfo.cardCode,
            cardStatus: this.type, // 2 激活  3 冻结
            remark: this.temp.remark
          }
          if (this.type === 3) {
            if (this.temp.remark === '') {
              this.$message({
                type: 'warning',
                message: '冻结当前充值卡,备注必须填写'
              })
              return
            }
          }
          var res1 = await topUpAdministrationChangeStatus(params1)
          if (res1.status === 200) {
            this.$message({
              type: 'success',
              message: '修改成功！'
            })
            this.visible = false
          }
          break
        case 4: // 换卡
          var _this = this
          _this.csmCardRWType = 1// 设置读卡器状态为读卡状态
          var isOpen = _this.obj.isSocketOpen()
          console.log('读卡状态..isOpen', isOpen)
          if (!isOpen) {
            this.$message({
              type: 'warning',
              message: '充值设备连接异常，请联系管理员'
            })
            _this.locked = false
            return
          }
          _this.readerIndex = 1
          if (_this.locked) {
            return
          }
          _this.locked = true
          _this.OpenCsmcard() // 打开读卡器
          break
        case 5: // 销卡
          if (!this.temp.money) {
            this.$message({
              type: 'warning',
              message: '请输入实退金额'
            })
            return
          }
          var params2 = {
            csmCode: this.csmInfo.csmCode,
            cardCode: this.csmInfo.cardCode,
            money5: this.csmInfo.cashMoney * 100,
            money6: this.temp.money * 100,
            remark: this.temp.remark
          }
          var money = params2.money6 - params2.money5
          if (money > 0) {
            this.$message({
              type: 'warning',
              message: '应退金额必须小于实退金额'
            })
            return
          }
          this.$confirm('确认销卡退款吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(async() => {
            var res2 = await topUpAdministrationMelt(params2)
            if (res2.status === 200) {
              this.$message({
                type: 'success',
                message: '销卡退款成功！'
              })
              this.visible = false
            }
          })
          break
        case 6: // 手机号变更
          if (this.csmInfo.csmPhone * 1 === this.csmPhone * 1) {
            this.$message({
              type: 'warning',
              message: '更换后的电话与原电话不能相同'
            })
            return
          }
          var params3 = {
            csmPhone: this.csmPhone,
            cardCode: this.csmInfo.cardCode,
            remark: this.temp.remark
          }
          var res2 = await topUpAdministratioCsmPhone(params3)
          if (res2.status === 200) {
            this.$message({
              type: 'success',
              message: '更改绑定手机号成功！'
            })
            this.visible = false
          }
          break
        case 7: // 卡密码修改
          this.$refs.password.validate().then(async(res) => {
            if (this.pas.password1 === this.pas.password2) {
              this.$message({
                type: 'warning',
                message: '储值卡修改密码相同'
              })
              return
            }
            if (this.pas.password2 !== this.pas.password3) {
              this.$message({
                type: 'warning',
                message: '确认免密与新密码输入不相同'
              })
              return
            }
            var params4 = {
              cardCode: this.csmInfo.cardCode,
              password1: this.pas.password1, // 原密码
              password2: this.pas.password2, // 新密码
              remark: this.temp.remark
            }
            var res3 = await topUpAdministratioPas(params4)
            if (res3.status === 200) {
              this.$message({
                type: 'success',
                message: '修改密码成功！'
              })
              this.visible = false
            }
          })
          break
        case 8: // 卡密码重置
          var params5 = {
            cardCode: this.csmInfo.cardCode,
            remark: this.temp.remark
          }
          var res4 = await topUpAdministratioReset(params5)
          if (res4.status === 200) {
            this.$message({
              type: 'success',
              message: '重置密码成功！'
            })
            this.visible = false
          }
          break
        case 9: // 调账冲正
          if ((this.price.changeCashMoney * 100) < 0) {
            this.$message({
              type: 'warning',
              message: '调整后现金卡值最小为0'
            })
            return
          }
          // 获取调整后赠送卡值
          if ((this.price.changeSendMoney * 100) < 0) {
            this.$message({
              type: 'warning',
              message: '调整后赠送卡值最小为0'
            })
            return
          }
          // 获取调整后卡余额/余次
          if ((this.price.changeCardSurplus * 100) < 0) {
            this.$message({
              type: 'warning',
              message: '调整后卡余额最小为0'
            })
            return
          }
          var params6 = {
            cardCode: this.csmInfo.cardCode,
            accCsmcardRecordList: [
              {
                feeCount1: this.curtainTy === 1 ? parseInt(this.price.updateCashMoney * 100) : parseInt(this.price.updateCashMoney * -100),
                feeCount2: this.curtainTy === 1 ? parseInt(this.price.updateSendMoney * 100) : parseInt(this.price.updateSendMoney * -100),
                remark: this.temp.remark
              }
            ]
          }
          var res5 = await topUpAdministratioAccount(params6)
          if (res5.status === 200) {
            this.$message({
              type: 'success',
              message: '调账冲正成功！'
            })
            this.visible = false
          }
          break
        }
      },
      reset() {
        this.visible = false
      },
      async formatInput(e) {
        if (this.type === 5) {
          this.temp.money = (e.target.value * 1).toFixed(2)
          return
        }
        if (e.target.value) {
          this.temp.feeCount1 = (e.target.value * 1).toFixed(2)
          const res = await topUpAdministrationPrice({ feeCount1: this.temp.feeCount1 * 100 })
          var result = res.data // 赠送金额
          if (result !== 0) {
            result = result * 1.00 / 100
            result = isNaN(result) ? '0.00' : result.toFixed(2)
          }
          // 赠送金额
          this.temp.feeCount2 = result === 0 ? 0.00 : result
          // 设置卡内余额
          this.temp.cardStatus = this.temp.feeCount1
          // 设置应收金额
          this.temp.totalPrice = (this.temp.feeCount1 * 1) + (result * 1) + (this.temp.cardFee * 1)
        }
      },
      // 请求用户信息接口
      async getUserInfo(cardCode) {
        const res = await topUpAdministrationInfoInfo({
          cardCode: cardCode
        })
        console.log('用户信息', res)
        this.csmInfo = res.data
        // 初始数据
        this.price.changeCashMoney1 = this.numberFormat1(this.csmInfo.cashMoney)// 初始数据现金卡值
        this.price.changeSendMoney1 = this.numberFormat1(this.csmInfo.sendMoney)// 初始数据赠送卡值调整
        this.price.changeCardSurplus1 = this.numberFormat1(this.csmInfo.cardSurplus)// 初始数据调整后卡余额/余次
        // 调整后
        this.price.changeCashMoney = this.numberFormat1(this.csmInfo.cashMoney)// 调整后现金卡值
        this.price.changeSendMoney = this.numberFormat1(this.csmInfo.sendMoney)// 调整后赠送卡值
        this.price.changeCardSurplus = this.numberFormat1(this.csmInfo.cardSurplus)// 调整后卡余额/余次
        // 查询客户状态
        var res1 = await topUpAdministrationUserInfo({
          csmStatus: 9, // 查询状态不是已注销的客户
          csmCode: this.csmInfo.csmCode
        })
        console.log('客户状态', res1)
        if (res1.data.csmStatus === 1) {
          this.$message({
            type: 'warning',
            message: '当前客户未审核'
          })
          return
        }
        if (res1.data.csmStatus === 3) {
          this.$message({
            type: 'warning',
            message: '当前客户已挂失'
          })
          return
        }
        if (res1.data.phoneItems.length) {
          this.phoneItems = res1.data.phoneItems.filter(item => {
            return item.cpStatus === 2
          })
          if (this.phoneItems.length > 0) {
            this.csmPhone = this.phoneItems[0].csmPhone
          }
        }
      },
      // 调账冲正
      onMoney(e, is) {
        if (!e.target.value) {
          e.target.value = 0.00
          e.target.value = 0.00
        }
        if (e.target.value) {
          if (is === 'updateCashMoney') {
            if (this.curtainTy === 1) {
              this.price.updateCashMoney = (e.target.value * 1).toFixed(2)
              // 调整后现金卡值
              this.price.changeCashMoney = ((this.price.updateCashMoney * 1) + (this.price.changeCashMoney1 * 1)).toFixed(2)
              // 调整后卡余额/余次
              this.price.changeCardSurplus = ((this.price.updateCashMoney * 1) + (this.price.changeCardSurplus1 * 1) + (this.price.updateSendMoney * 1)).toFixed(2)
            } else {
              this.price.updateCashMoney = (e.target.value * 1).toFixed(2)
              // 调整后现金卡值
              this.price.changeCashMoney = ((this.price.changeCashMoney1 * 1) - (this.price.updateCashMoney * 1)).toFixed(2)
              // 调整后卡余额/余次
              this.price.changeCardSurplus = ((this.price.changeCardSurplus1 * 1) - (this.price.updateCashMoney * 1) - (this.price.updateSendMoney * 1)).toFixed(2)
            }
          } else {
            if (this.curtainTy === 1) {
              this.price.updateSendMoney = (e.target.value * 1).toFixed(2)
              // 赠送卡值调整
              this.price.changeSendMoney = ((this.price.updateSendMoney * 1) + (this.price.changeSendMoney1 * 1)).toFixed(2)
              // 调整后卡余额/余次
              this.price.changeCardSurplus = ((this.price.updateCashMoney * 1) + (this.price.changeCardSurplus1 * 1) + (this.price.updateSendMoney * 1)).toFixed(2)
            } else {
              this.price.updateSendMoney = (e.target.value * 1).toFixed(2)
              // 赠送卡值调整
              this.price.changeSendMoney = ((this.price.changeSendMoney1 * 1) - (this.price.updateSendMoney * 1)).toFixed(2)
              // 调整后卡余额/余次
              this.price.changeCardSurplus = ((this.price.changeCardSurplus1 * 1) - (this.price.updateSendMoney * 1) - (this.price.updateCashMoney * 1)).toFixed(2)
            }
          }
        }
      },
      //
      changeGroup(val) {
        if (val === 1) {
          this.price.updateCashMoney = (this.price.updateCashMoney * 1).toFixed(2)
          // 调整后现金卡值
          this.price.changeCashMoney = ((this.price.updateCashMoney * 1) + (this.price.changeCashMoney1 * 1)).toFixed(2)
          // 调整后赠送卡值
          this.price.changeSendMoney = ((this.price.updateSendMoney * 1) + (this.price.changeSendMoney1 * 1)).toFixed(2)
          // 调整后卡余额
          this.price.changeCardSurplus = ((this.price.updateCashMoney * 1) + (this.price.changeCardSurplus1 * 1) + (this.price.updateSendMoney * 1)).toFixed(2)
        } else {
          this.price.updateCashMoney = (this.price.updateCashMoney * 1).toFixed(2)
          // 调整后现金卡值
          this.price.changeCashMoney = ((this.price.changeCashMoney1 * 1) - (this.price.updateCashMoney * 1)).toFixed(2)
          // 调整后赠送卡值
          this.price.changeSendMoney = ((this.price.changeSendMoney1 * 1) - (this.price.updateSendMoney * 1)).toFixed(2)
          // 调整后卡余额
          this.price.changeCardSurplus = ((this.price.changeCardSurplus1 * 1) - (this.price.updateCashMoney * 1) - (this.price.updateSendMoney * 1)).toFixed(2)
        }
      },
      // 充值金额
      onInput(value, is) {
        const res = value
          .replace(/[^\d.]/g, '')
          .replace(/\.{2,}/g, '.')
          .replace(/^\./g, '')
          .replace('.', '$#$')
          .replace(/\./g, '')
          .replace('$#$', '.')
          .replace(/^0\d+|^\./g, '')
          .replace(/^(.*\..{2}).*$/, '$1')
        if (this.type === 9) {
          is === 'updateCashMoney' ? this.price.updateCashMoney = res : this.price.updateSendMoney = res
          return
        }
        this.temp.feeCount1 = res
        this.temp.money = res
      },
      // 初始化
      initCardServer() {
        var _this = this
        _this.obj = SmartReader.OBJ()
        _this.obj.createSocket()
        _this.obj.onResult(async(rData) => {
          console.log('进入的状态,rData.FunctionID', rData)
          console.log('进入的状态,rData.FunctionID', rData.FunctionID)
          switch (rData.FunctionID) {
          case FUNCIDS._fid_initialcom:
            var rel = Number(rData.RePara_Int)
            if (rel === 0) {
              var hdev = parseInt(rData.RePara_Str)
              if (hdev !== -1) {
                _this.icdev = hdev
                _this.obj.beep(_this.icdev, 10) // do a beep
                _this.isComOpen = true // Set reader link status
                // _this.readerCardCode = "";
                _this.LoadKey() // 验证密码
              } else {
                _this.locked = false
                _this.$message({
                  type: 'warning',
                  message: '打开读卡器失败'
                })
                return
              }
            } else {
              _this.locked = false
              _this.$message({
                type: 'warning',
                message: '打开读卡器失败'
              })
              return
            }
            break
          case FUNCIDS._fid_exit: // 关闭读卡器
            break
          case FUNCIDS._fid_beep: // 峰槽声
            break
          case FUNCIDS._fid_loadKey: // 校验密码
            if (_this.csmCardRWType === 1) { // 说明是读卡状态
              _this.Read()
            } else if (_this.csmCardRWType === 2) { // 写卡充能
              _this.Write()
            } else if (_this.csmCardRWType === 3) { // 清卡功能
              _this.Write()
            }
            break
          case FUNCIDS._fid_findCardStr: // 读取操作进入
            var strcard = rData.RePara_Str
            if (strcard !== '') {
              _this.hasCard = true
              switch (_this.gl_wantFunc) {
              case _this.readClass:
              case _this.writeClass:
                _this.obj.authentication(_this.icdev, 0, _this.gl_sector)
                break
              }
            } else {
              _this.$message({
                type: 'warning',
                message: '充值设备连接异常，请联系管理员'
              })
              _this.locked = false
              _this.CloseCsmcard()
              return
            }
            break
          case FUNCIDS._fid_authenClass: // 读取
            var rel1 = Number(rData.RePara_Int)
            if (rel1 === 0) {
              switch (_this.gl_wantFunc) {
              case _this.readClass: // 读卡号
                // 读取卡号
                if (_this.readerIndex === 1) {
                  _this.obj.read(_this.icdev, _this.gl_BinBlock)
                  // 读取卡第二扇区值
                } else if (_this.readerIndex === 2) {
                  _this.obj.read(_this.icdev, 2)
                }

                break
              case _this.writeClass: // 写卡号
                var params = {
                  readerCardCode: _this.readerCardCode,
                  readerStrCode: _this.readerStrCode,
                  cardCode: _this.csmInfo.cardCode,
                  cardStrCode: _this.writerNowDate,
                  remark: this.temp.remark
                }
                console.log('....换卡入餐', params)
                try {
                  var res = await topUpAdministratioTradeCrad(params)
                  console.log('....换卡出餐', res)
                  _this.locked = false
                  _this.CloseCsmcard()
                  this.$message({
                    type: 'success',
                    message: '换卡成功！'
                  })
                } catch (error) {
                  _this.locked = false
                  _this.CloseCsmcard()
                  // _this.obj.write(_this.icdev, _this.gl_BinBlock, _this.writerCsmCard)
                  // 写入第二扇区值
                  // _this.obj.write(_this.icdev, 2, _this.writerNowDate)
                }
                break
              }
            } else {
              _this.$message({
                type: 'warning',
                message: '充值设备连接异常，请联系管理员'
              })
              _this.locked = false
              _this.CloseCsmcard()
              return
            }
            break
          case FUNCIDS._fid_readAsStr: // 输出读取的卡号
            var data = rData.RePara_Str
            if (data !== '') {
              if (_this.readerIndex === 1) {
                _this.readerCardCode = data
                // 处理卡号
                if (_this.readerCardCode === '') {
                  _this.$message({
                    type: 'warning',
                    message: '读取卡号失败'
                  })
                  _this.locked = false
                  _this.CloseCsmcard()
                  return
                }

                var strArr = _this.readerCardCode.split('')
                var newCardCode = ''
                for (var x = 0; x < strArr.length; x++) {
                  if (strArr[x] !== 'A') {
                    newCardCode = newCardCode + strArr[x]
                  }
                }
                _this.readerCardCode = newCardCode
                console.log('读取的卡号_this.readerCardCode', _this.readerCardCode)
                _this.CloseCsmcard()
                // 读取老卡第二扇区值
                _this.readerCardInfo(2)
                // 读取第二扇区值
              } else if (_this.readerIndex === 2) {
                _this.readerStrCode = data
                console.log('第二三区的值_this.readerStrCode', _this.readerStrCode)
                // 读取完成第二扇区随机数,将readerIndex值设置成默认值
                _this.readerIndex = 1
                _this.CloseCsmcard()
                // 写入卡片
                _this.writerCardCode()
              }
            } else {
              _this.$message({
                type: 'warning',
                message: '充值设备连接异常，请联系管理员'
              })
              _this.locked = false
              _this.CloseCsmcard()
              return
            }
            break
          case FUNCIDS._fid_writeAsStr: // 输出写入的卡号
            var iRet = Number(rData.RePara_Int)
            if (iRet === 0) {
              // 写入卡片成功
              _this.CloseCsmcard()
            } else {
              _this.$message({
                type: 'warning',
                message: '写入卡片失败'
              })
              _this.locked = false
              _this.CloseCsmcard()
              return
            }
            break
          }
        })
      },
      // 校验A密码
      LoadKey() {
        var _this = this
        _this.obj.loadkey(_this.icdev, 0, _this.gl_sector, 'FFFFFFFFFFFf') // Load key, 0: keyA, ffffffffffff:key
      },

      // 读卡
      Read() {
        var _this = this
        _this.obj.findcardStr(_this.icdev, 0)// 1);     //1: multy card mode
        _this.gl_wantFunc = _this.readClass
      },

      // 写卡
      Write() {
        var _this = this
        _this.obj.findcardStr(_this.icdev, 0)// 1);     //1: multy card mode
        _this.gl_wantFunc = _this.writeClass
      },

      // 关闭读卡器
      CloseCsmcard() {
        var _this = this
        _this.obj.exit(_this.icdev)
        _this.isComOpen = false // Set unlink status
      },
      // 打开读卡器
      OpenCsmcard() {
        var _this = this
        try {
          if (_this.isComOpen === false) {
            console.log('打开读卡器', _this.isComOpen)
            _this.obj.initialcom(100, 0)
          }
        } catch (e) {
          alert(e.message)
        }
        return
      },
      // 读取第二扇区值
      readerCardInfo(index) {
        var _this = this
        _this.csmCardRWType = 1// 设置读卡器状态为读卡状态
        _this.readerIndex = index
        _this.OpenCsmcard() // 打开读卡器
      },
      /** 写入卡片**/
      writerCardCode() {
        var _this = this
        // 获取卡编号
        var cardCode = _this.csmInfo.cardCode
        var cardCodeLength = cardCode.length
        var forLength = 32 - cardCodeLength
        for (var x = 0; x < forLength; x++) {
          cardCode = cardCode + 'a'
        }
        _this.writerCsmCard = cardCode
        // 当前办卡时间戳
        var dateTime = Date.now() + ''
        var forDateTimeLength = 32 - dateTime.length
        for (var a = 0; a < forDateTimeLength; a++) {
          dateTime = dateTime + 'b'
        }
        _this.writerNowDate = dateTime
        _this.csmCardRWType = 2// 设置读卡器状态为写卡状态
        _this.OpenCsmcard() // 打开读卡器
      },
      // 充值金额
      numberFormat1: function(val) {
        val = val * 1.00 / 100
        val = Number(val)
        val = isNaN(val) ? '0.00' : val.toFixed(2)
        return val
      }
    }
  }
</script>

<style lang="scss" scoped>
.item {
  margin-bottom: 30px;
  .title {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    .title-icon {
      display: inline-block;
      width: 4px;
      height: 18px;
      margin-right: 6px;
      background: #01999f;
    }
    .title-text {
      font-family: Microsoft YaHei, Microsoft YaHei-Bold;
      font-size: 14px;
      font-weight: 700;
      color: #01999f;
    }
  }
  &:last-child {
    margin-bottom: 0;
  }
  .content {
    margin-bottom: 30px;
  }
  .form {
    display: flex;
    flex-wrap: wrap;
    & /deep/ .el-form-item {
      display: flex;
      width: 50%;
      padding-right: 10px;
      .el-form-item__content {
        width: 100%;
      }
    }
    /deep/.el-form-item__content {
      margin-left: 10px !important;
    }
    span {
      margin-left: 6px;
      span {
        font-size: 14px;
        color: red;
      }
    }
  }
}
</style>
